package com.qfedu.hotelssm.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.qfedu.hotelssm.dao.HqUseCarDao;
import com.qfedu.hotelssm.dto.HqUseCarExportDto;
import com.qfedu.hotelssm.entity.HqUseCar;
import com.qfedu.hotelssm.service.intf.HqUseCarService;
import com.qfedu.hotelssm.util.StringUtil;
import com.qfedu.hotelssm.vo.PageBean;
import com.qfedu.hotelssm.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @author zlp
 * @date 2022/8/29 20:26
 */
@Service
public class HqUseCarServiceImpl implements HqUseCarService {
    @Autowired
    private HqUseCarDao dao;

    @Override
    public R save(HqUseCar car) {
        if (dao.save(car) > 0) {
            return R.ok();
        } else {
            return R.fail();
        }
    }

    @Override
    public R delete(int id) {
        if (dao.delete(id) > 0) {
            return R.ok();
        } else {
            return R.fail();
        }
    }

    @Override
    public R update(HqUseCar car) {
        if (dao.update(car) > 0) {
            return R.ok();
        } else {
            return R.fail();
        }
    }

    @Override
    public PageBean pageByName(String roomid, int page, int limit) {
        if (StringUtil.isNotEmpty(roomid)) {
            roomid = "%" + roomid + "%";
        }
        int start = (page - 1) * limit;
        return  new PageBean((int) dao.selectCount(roomid), dao.selectPage(roomid,start,limit));
    }

    @Override
    public List<HqUseCarExportDto> all() {
        return dao.all();
    }

    @Override
    public R upload(MultipartFile file) throws IOException {
        if(!file.isEmpty()){
            List<HqUseCar> list= EasyExcel.read(file.getInputStream(), HqUseCar.class, new ReadListener() {
                @Override
                public void invoke(Object o, AnalysisContext analysisContext) {
                }
                @Override
                public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                }
            }).sheet().doReadSync();
            if(dao.saveBatch(list)>0 ){
                return  R.ok();
            }
        }
        return R.fail();
    }

    @Override
    public R delByIds(int[] ids) {
        if (dao.delByIds(ids) > 0) {
            return R.ok();
        } else {
            return R.fail();
        }
    }
}
